var message = "";
var isSubmit = ["0","0","0","0","0"];
$(
	function()
	{
		$("#userName").blur
		(
				function()
				{
					//定义一个变量用来存放正则字符串
					var reg = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
					var userNameValue = $("#userName").val();
			
					var flag = false;
							
					
					//完成正则判断
					if(!reg.test(userNameValue))
					{
						message = "邮箱不合法！";
						isSubmit[0] = 0;
					}
					else
					{
						isSubmit[0] = 1;
						flag = true;		
					}
					
					
					if(flag)
					{

						$("#img1").attr('src','/ShoppingWebsite/images/right.jpg');
						$('#reg_sp1').text("")

						
					}
					else
					{
						
						$("#img1").attr('src','/ShoppingWebsite/images/wrong.jpg');
						$('#reg_sp1').text(message).css('color','red');
					}
					
					//用ajax判断邮箱是否已经被注册
					if(isSubmit[0] == 1)
					{
						var json = {"userName":$("#userName").val()};
						$.get("/ShoppingWebsite/RegisterAjaxServlet",json,function(data,suss)
						{
							if(data == "true")
							{
								message = "该邮箱已被注册";
								isSubmit[0] = 0;
								$("#img1").attr('src','/ShoppingWebsite/images/wrong.jpg');
								$('#reg_sp1').text("该邮箱已被注册!").css('color','red');
							}
						}
						);
					}
				}
		);
		
		$("#password").blur
		(
			function()
			{
				//记录输入的长度
				var passwordNum = $("#password").val().length;
				//用于匹配密码和确认密码的输入  用于同时修改两个栏的图标
				var password = $("#password").val();
				var password2 = $("#password2").val();
				var num = $("#password2").val().length;
					
				//修改密码栏的值
				if(passwordNum < 6)
				{
					isSubmit[1] = 0;
					$("#img2").attr('src','/ShoppingWebsite/images/wrong.jpg');
					$('#reg_sp2').text("密码长度不得小于六位！").css('color','red');
				}
				else
				{
					isSubmit[1] = 1;
					$("#img2").attr('src','/ShoppingWebsite/images/right.jpg');
					$('#reg_sp2').text("")
				}
				
				//修改确认密码栏的值
				if(password2 != password)
				{
					isSubmit[2] = 0;
					$("#img3").attr('src','/ShoppingWebsite/images/wrong.jpg');
					$('#reg_sp3').text("密码不相同！").css('color','red');
				}
				else if(password2 == password)
				{
					isSubmit[2] = 1;
					$("#img3").attr('src','/ShoppingWebsite/images/right.jpg');
					$('#reg_sp3').text("");
				}
			}
		);
		
		$("#password2").blur
		(
			function()
			{
				var password = $("#password").val();
				var password2 = $("#password2").val();
				var num = $("#password2").val().length;
				
				if(password2 != password)
				{
					isSubmit[2] = 0;
					$("#img3").attr('src','/ShoppingWebsite/images/wrong.jpg');
					$('#reg_sp3').text("密码不相同！").css('color','red');
				}
				else if(num > 0)
				{
					isSubmit[2] = 1;
					$("#img3").attr('src','/ShoppingWebsite/images/right.jpg');
					$('#reg_sp3').text("");
				}
			}
		);
		
		$("#realName").blur
		(
			function()
			{
				var name = $("#realName").val();
				var reg = /^[\u0391-\uFFE5]+$/;    
				
				if(!reg.test(name))
				{
					isSubmit[3] = 0;
					$("#img4").attr('src','/ShoppingWebsite/images/wrong.jpg');
					$('#reg_sp4').text("请输入真实姓名！").css('color','red');
				}
				else
				{
					isSubmit[3] = 1;
					$("#img4").attr('src','/ShoppingWebsite/images/right.jpg');
					$('#reg_sp4').text("");
				}
			}
		);
			
		$("#phone").blur
		(
			function()
			{
				var phone = $("#phone").val();
				var reg = /^1(3|4|5|7|8)\d{9}$/;
				
				if(!reg.test(phone))
				{
					isSubmit[4] = 0;
					$("#img7").attr('src','/ShoppingWebsite/images/wrong.jpg');
					$('#reg_sp7').text("手机号码不合法！").css('color','red');
				}
				else
				{
					isSubmit[4] = 1;
					$("#img7").attr('src','/ShoppingWebsite/images/right.jpg');
					$('#reg_sp7').text("");
				}
			}
		);
		
		function formSubmit()
		{ 
			document.loginInfo.submit();
		}
	}
);

function show()
{
	$("#CheckImg").attr("src","/ShoppingWebsite/CheckCoed?time=" + new Date().getTime());
}

//判断输入框是否合法
function toVaild()
{
	var count = 0;
	for(var i = 0 ; i < isSubmit.length ; i++)
	{
		if(isSubmit[i] == 1)
		{
			count++;
		}
		else if(isSubmit[i] == 0)
		{
			var message = "";
			switch(count)
			{
			 	case 0: message = "邮箱错误"; 
			 	break; 
			 	case 1:x= message = "密码输入错误";
			 	break; 
			 	case 2:x= message = "确认密码错误";
			 	break; 
			 	case 3:x= message = "请输入中文真实姓名";
			 	break; 
			 	case 4:x= message = "请输入正确手机号码";
			 	break; 
			}
			
		}
	}
	
	if(count == isSubmit.length)
	{
		return true;
	}
	
	alert(message);
	return false;
}